﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace LeakView.misc.view.controls
{
    public partial class HistoryChartDouble : UserControl
    {

        [Browsable(true)]
        [DescriptionAttribute("设置或获取标题")]
        public string Title
        {
            get => this.chart1.Titles.First().Text;
            set => this.chart1.Titles.First().Text = value;
        }


        public HistoryChartDouble()
        {
            InitializeComponent();

            this.chart1.Series[0].Color = Miscs.HeadColor;
            this.chart1.Series[1].Color = Miscs.TailColor;
            this.chart1.Series[0].LegendText = AppConfig.HeadName;
            this.chart1.Series[1].LegendText = AppConfig.TailName;
        }

        private void ChartMiscHistory_Load(object sender, EventArgs e)
        {
            this.chart1.ChartAreas.First().CursorX.IsUserEnabled = true;
            this.chart1.ChartAreas.First().CursorY.IsUserEnabled = true;

            this.chart1.ChartAreas.First().CursorX.Interval = 1;
            this.chart1.ChartAreas.First().CursorX.IntervalType = DateTimeIntervalType.Seconds;
            this.chart1.ChartAreas.First().CursorX.LineWidth = 0;

            this.chart1.ChartAreas.First().CursorY.Interval = 0.001;
            this.chart1.ChartAreas.First().CursorY.IntervalType = DateTimeIntervalType.Number;
            this.chart1.ChartAreas.First().CursorY.LineWidth = 0;

            chart1.ChartAreas[0].AxisX.IsStartedFromZero = false;
            chart1.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm"; //X轴显示的时间格式，HH为大写时是24小时制，hh小写时是12小时制
            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Hours;//如果是时间类型的数据，间隔方式可以是秒、分、时
            chart1.ChartAreas[0].AxisX.Interval = 2;//间隔为2小时

            // 设置当前数值为空
            chart1.Titles["info"].Text = "";
        }

        public void SetTimeRange(DateTime min, DateTime max)
        {
            var span = max - min;
            chart1.ChartAreas[0].AxisX.Minimum = min.ToOADate();
            chart1.ChartAreas[0].AxisX.Maximum = max.ToOADate();

            chart1.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Minutes;
            chart1.ChartAreas[0].AxisX.Interval = span.TotalMinutes / 6;
        }

        private void appendValue(int idx, DateTime time, double value, bool valid)
        {
            var index = this.chart1.Series[idx].Points.AddXY(time, value);
            if (!valid)
            {
                this.chart1.Series[idx].Points[index].IsEmpty = true;
            }
        }

        public void appendValue(int index, DateTime time, double value)
        {
            if (double.IsNaN(value))
            {
                appendValue(index, time, 0.0, false);
            }
            else
            {
                appendValue(index, time, value, true);
            }
        }

        public void clear()
        {

            // 清空数据
            this.chart1.Series[0].Points.Clear();
            this.chart1.Series[1].Points.Clear();

            // 设置当前数值为空
            chart1.Titles["info"].Text = "";
        }

        private void chart1_MouseMove(object sender, MouseEventArgs e)
        {
            var area = chart1.ChartAreas.First();

            // 显示红色游标线
            area.CursorX.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
            area.CursorY.SetCursorPixelPosition(new PointF(e.X, e.Y), true);
        }

        private void chart1_MouseEnter(object sender, EventArgs e)
        {
            this.timer1.Interval = 50;
            this.timer1.Enabled = true;

            this.chart1.ChartAreas.First().CursorX.LineWidth = 1;
            this.chart1.ChartAreas.First().CursorY.LineWidth = 1;
            //this.chart1.ChartAreas.First().CursorX.
        }

        private void chart1_MouseLeave(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            chart1.Titles["info"].Text = string.Format("");

            this.chart1.ChartAreas.First().CursorX.LineWidth = 0;
            this.chart1.ChartAreas.First().CursorY.LineWidth = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            var area = chart1.ChartAreas.First();

            Point currentPosition = Control.MousePosition;
            currentPosition = PointToClient(currentPosition);

            //this.chart1.ChartAreas.First().CursorX
            double mouseMove_X = area.AxisX.PixelPositionToValue(currentPosition.X);
            double mouseMove_Y = area.AxisY.PixelPositionToValue(currentPosition.Y);

            DateTime time = DateTime.FromOADate(mouseMove_X);
            chart1.Titles["info"].Text = string.Format("{0}       {1:F3}", time, mouseMove_Y);
        }
    }
}
